package com.renbang.action.devoloper;

import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.renbang.model.common.AjaxResult;
import com.renbang.model.common.Page;
import com.renbang.model.sys.SysBtn;
import com.renbang.model.sys.SysFun;
import com.renbang.service.sys.SysFunServiceImp;

@Controller
@RequestMapping(value = "developer/fun")
public class SysFunAction  {

	@Autowired
	SysFunServiceImp sysFunServiceImp;

	@RequestMapping(value = "/showList")
	public @ResponseBody AjaxResult showList(HttpServletRequest request,
			SysFun sysFun, Page page) {
		
		Integer count = sysFunServiceImp.findTotal(sysFun);
		return new AjaxResult(AjaxResult.STATUS_OK, page.getCurrpage()+"",null ,sysFunServiceImp.findInPage(sysFun,page), count);
	}

	@RequestMapping(value = "/addOrModify")
	public @ResponseBody AjaxResult addOrModify(SysFun sysFun, String[] funBtns) {
		if (sysFun.getId() != null) {
			sysFunServiceImp.modify(sysFun);
			return new AjaxResult(AjaxResult.STATUS_OK, null, "修改成功");
		} else {
			sysFunServiceImp.add(sysFun);
			 return new AjaxResult(AjaxResult.STATUS_OK, null, "增加成功");
		}
	}

	@RequestMapping(value = "/del")
	public @ResponseBody AjaxResult del(String keys) {
		sysFunServiceImp.removeById(Integer.valueOf(keys));
		return new AjaxResult("", null, "");
	}

	@RequestMapping(value = "/editForm")
	public String editForm(Integer id, HttpServletRequest request) {
		SysFun sysFun;
		if (id != null) {
			sysFun = sysFunServiceImp.findById(id);
			request.setAttribute("obj", sysFun);
		}
		return "developer/fun-form";
	}

	@RequestMapping(value = "/all")
	public @ResponseBody List<SysFun> all() {
		return 	sysFunServiceImp.findByModel(null);
	}

	@RequestMapping(value = "/findById")
	public @ResponseBody AjaxResult findById(Integer funId) {
		System.out.println(funId);
		SysFun sysFun = sysFunServiceImp.findById(funId);
		System.out.println(sysFun);
		return new AjaxResult(AjaxResult.STATUS_OK, null, null,
				Arrays.asList(sysFun), 0);
	}
}
